<?php
/**
 * feeyoAirportCode.php
 * 描述
 *
 * 作者:  胡毅 (huyi@comsenz.com)
 * 创建时间: 2012-10-13 下午4:20:20
 * 修改记录:
 *
 * $id$
 */

class Flight_Service_Crawl_FeeyoAirportCode extends Service {

    /**
     * crawlAirportCode
     * 抓取机场信息
     */
    public function crawlAirportCode() {

        $config = Config::get('Flight.crawl_airport.feeyo.airpode_codes');

        // 开始抓取
        try {
            // 是否把config解析成了多个,如果解析成了多个,就需要一次循环
            list($newConfig, $isMulti) = Flight_Helper_CrawlConfig::parseConfig($config);
            $configArray = $newConfig;
            if(!$isMulti) {
                $configArray = array($newConfig);
            }
            foreach ($configArray as $crawlConfig) {
                $crawlClient = new Flight_FlightCrawl_Client($crawlConfig);
                $crawlData = $crawlClient->crawl();
                $saveData = array();
                foreach ($crawlData as $key => $value) {
                    foreach ($value as $k => $v) {
                        $saveData[$k][$key] = $v;
                    }
                }
                $this->_save($saveData);
            }
        } catch (Exception $e) {
            Logger::error($e);
            throw new Service_Exception($e);
        }
        return true;
    }

    private function _save($data) {

        try {
            $airportCodeService =& Service::factory('Flight_Service_AirportCode');
            foreach ($data as $d) {
                $threeCode = $d['threeCode'];
                $airData = $airportCodeService->get($threeCode);
                if(!$airData) {
                    if(strtolower(substr($d['fourCode'], 0, 1)) == 'z') {
                        if($d['threeCode']) {
                            $addData = array(
                                    'threeCode' => $d['threeCode'],
                                    'fourCode' => $d['fourCode'],
                                    'acAirpotName' => $d['acAirpotName'],
                                    'acCityName' => $d['acCityName'],
                                    'acCityPinyin' => $d['acCityPinyin'],
                                    'acCityEnName' => $d['acCityPinyin'],
                            );
                            $airportCodeService->add($addData);
                        }
                        Logger::writeln('/airport/noThreeCode.txt', $threeCode . "\t{$d['fourCode']}\t{$d['acAirpotName']}");
                        echo $threeCode . "\tnot find" .PHP_EOL;
                    }
                    continue;
                }
                $updateData = array(
                        'fourCode' => $d['fourCode'],
                        'acAirpotName' => $d['acAirpotName'],
                        'acCityName' => $d['acCityName'],
                        'acCityPinyin' => $d['acCityPinyin'],
                        'acCityEnName' => $d['acCityPinyin'],
                        );
                $airportCodeService->update($threeCode, $updateData);
                echo '.';
            }
        } catch (Exception $e) {
            Logger::error($e);
            throw new Service_Exception($e);
        }
    }
}